﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMK.Utils;
using System.Data;
using XMK.Tadong.Model;
using XMK.Framework.DataAccess;
using XMK.Tadong.Model.Cond;

namespace XMK.Tadong.Data
{
    public class Base_OrganizationData
    {
         /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public Base_OrganizationData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        /// <summary>
        /// 职工组织机构列表提取
        /// </summary>
        /// <returns></returns>
        public List<Base_OrganizationInfo> GetStaffOrganizeList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT Organization_ID,Organization_Name,ParentId,'0' AS isUser FROM Base_Organization UNION ALL SELECT U.User_ID AS Organization_ID ,U.User_Code || '|' || U.User_Name AS User_Name,S.Organization_ID,'1' AS isUser FROM Base_UserInfo U RIGHT JOIN Base_StaffOrganize S ON U.User_ID = S.User_ID");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            List<Base_OrganizationInfo> staffOrgList = DbManager.GetInfoList<Base_OrganizationInfo>(cmd, 
                delegate(out Base_OrganizationInfo staffOrg, DataRow row)
            {
                staffOrg = new Base_OrganizationInfo
                {
                    ORGANIZATION_ID = ParseHelper.Parse<string>(row["Organization_ID"]),
                    ORGANIZATION_NAME = ParseHelper.Parse<string>(row["Organization_Name"]),
                    PARENTID = ParseHelper.Parse<string>(row["ParentId"]),
                    IsUser = ParseHelper.Parse<string>(row["isUser"]).Equals("0") ? false:true
                };
            });
            return staffOrgList;
        }

        //#region test

        /// <summary>
        /// 组织机构数据提取
        /// </summary>
        /// <returns></returns>
        public List<Base_OrganizationInfo> GetOrganizeList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM Base_Organization WHERE DeleteMark = 1 ORDER BY SortCode ASC");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            List<Base_OrganizationInfo> staffOrgList = DbManager.GetInfoList<Base_OrganizationInfo>(cmd,
                delegate(out Base_OrganizationInfo staffOrg, DataRow row)
                {
                    staffOrg = new Base_OrganizationInfo
                    {
                        ORGANIZATION_ID = ParseHelper.Parse<string>(row["Organization_ID"]),
                        ORGANIZATION_CODE = ParseHelper.Parse<string>(row["ORGANIZATION_CODE"]),
                        ORGANIZATION_NAME = ParseHelper.Parse<string>(row["Organization_Name"]),
                        ORGANIZATION_INNERPHONE = ParseHelper.Parse<string>(row["ORGANIZATION_INNERPHONE"]),
                        ORGANIZATION_OUTERPHONE = ParseHelper.Parse<string>(row["ORGANIZATION_OUTERPHONE"]),
                        ORGANIZATION_MANAGER = ParseHelper.Parse<string>(row["ORGANIZATION_MANAGER"]),
                        ORGANIZATION_ASSISTANTMANAGER = ParseHelper.Parse<string>(row["ORGANIZATION_ASSISTANTMANAGER"]),
                        ORGANIZATION_FAX = ParseHelper.Parse<string>(row["ORGANIZATION_FAX"]),
                        ORGANIZATION_ZIPCODE = ParseHelper.Parse<string>(row["ORGANIZATION_ZIPCODE"]),
                        ORGANIZATION_ADDRESS = ParseHelper.Parse<string>(row["ORGANIZATION_ADDRESS"]),
                        PARENTID = ParseHelper.Parse<string>(row["ParentId"]),
                        ORGANIZATION_REMARK = ParseHelper.Parse<string>(row["ORGANIZATION_REMARK"]),
                        SORTCODE = ParseHelper.Parse<decimal>(row["SORTCODE"]),
                        DELETEMARK = ParseHelper.Parse<decimal>(row["DELETEMARK"]),
                        CREATEDATE = ParseHelper.ParseDateTimeToStr(ParseHelper.Parse<DateTime>(row["CREATEDATE"])),
                        CREATEUSERID = ParseHelper.Parse<string>(row["CREATEUSERID"]),
                        CREATEUSERNAME = ParseHelper.Parse<string>(row["CREATEUSERNAME"]),
                        MODIFYDATE =ParseHelper.ParseDateTimeToStr(ParseHelper.Parse<DateTime>(row["MODIFYDATE"])),
                        MODIFYUSERID = ParseHelper.Parse<string>(row["MODIFYUSERID"]),
                        MODIFYUSERNAME = ParseHelper.Parse<string>(row["MODIFYUSERNAME"])
                    };
                });
            return staffOrgList;
        }

        /// <summary>
        /// 根据部门id获取部门信息
        /// </summary>
        /// <returns></returns>
        public Base_OrganizationInfo getOrgInfoById(string orgId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT * FROM base_organization WHERE ORGANIZATION_ID = '"+orgId+"'");

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            Base_OrganizationInfo Obj = DbManager.GetInfo(cmd,
                delegate(out Base_OrganizationInfo u, DataRow row)
                {
                    u = new Base_OrganizationInfo
                    {
                        ORGANIZATION_ID = ParseHelper.Parse<string>(row["Organization_ID"]),
                        ORGANIZATION_CODE = ParseHelper.Parse<string>(row["ORGANIZATION_CODE"]),
                        ORGANIZATION_NAME = ParseHelper.Parse<string>(row["Organization_Name"]),
                        ORGANIZATION_INNERPHONE = ParseHelper.Parse<string>(row["ORGANIZATION_INNERPHONE"]),
                        ORGANIZATION_OUTERPHONE = ParseHelper.Parse<string>(row["ORGANIZATION_OUTERPHONE"]),
                        ORGANIZATION_MANAGER = ParseHelper.Parse<string>(row["ORGANIZATION_MANAGER"]),
                        ORGANIZATION_ASSISTANTMANAGER = ParseHelper.Parse<string>(row["ORGANIZATION_ASSISTANTMANAGER"]),
                        ORGANIZATION_FAX = ParseHelper.Parse<string>(row["ORGANIZATION_FAX"]),
                        ORGANIZATION_ZIPCODE = ParseHelper.Parse<string>(row["ORGANIZATION_ZIPCODE"]),
                        ORGANIZATION_ADDRESS = ParseHelper.Parse<string>(row["ORGANIZATION_ADDRESS"]),
                        PARENTID = ParseHelper.Parse<string>(row["ParentId"]),
                        ORGANIZATION_REMARK = ParseHelper.Parse<string>(row["ORGANIZATION_REMARK"]),
                        SORTCODE = ParseHelper.Parse<decimal>(row["SORTCODE"]),
                        DELETEMARK = ParseHelper.Parse<decimal>(row["DELETEMARK"]),
                        CREATEDATE = ParseHelper.ParseDateTimeToStr(ParseHelper.Parse<DateTime>(row["CREATEDATE"])),
                        CREATEUSERID = ParseHelper.Parse<string>(row["CREATEUSERID"]),
                        CREATEUSERNAME = ParseHelper.Parse<string>(row["CREATEUSERNAME"]),
                        MODIFYDATE = ParseHelper.ParseDateTimeToStr(ParseHelper.Parse<DateTime>(row["MODIFYDATE"])),
                        MODIFYUSERID = ParseHelper.Parse<string>(row["MODIFYUSERID"]),
                        MODIFYUSERNAME = ParseHelper.Parse<string>(row["MODIFYUSERNAME"])


                    };
                });
            return Obj;
        }
        /// <summary>
        /// 获取当前用户直属单位名-单位级别为“单位”
        /// </summary>
        /// <param name="orgID"></param>
        /// <returns></returns>
        public DataTable getOrgNameByUserId(string USER_ACCOUNT)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" select ORGANIZATION_NAME from  ( select * from base_organization　start with organization_id = (select t.organization_id from base_stafforganize t where t.USER_ID='" + USER_ACCOUNT + "'");
            strSql.Append(")connect by prior  parentid =organization_id) tmp where tmp.ORGANIZATION_LVL=2");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }
    }
}
